package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.repository.SystemUserRepository;
import com.ts.proj.service.SystemUserService;
import com.ts.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class SystemUserServiceImpl extends CommonServiceImpl<SystemUserEntity> implements SystemUserService {

    @Autowired
    private SystemUserRepository systemUserRepository;


    @Override
    public List<SystemUserEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemUserEntity> sp = this.getSpecification(pageData,SystemUserEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return systemUserRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemUserEntity> sp = this.getSpecification(pageData,SystemUserEntity.class);
        return systemUserRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<SystemUserEntity> iterable) {
        systemUserRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SystemUserEntity> saveAll(List<SystemUserEntity> systemUserEntities) {
        return systemUserRepository.saveAll(systemUserEntities);
    }

    @Override
    public List<SystemUserEntity> findAllById(Integer[] ids) {
        List<SystemUserEntity> list = systemUserRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemUserEntity systemUserEntity = new SystemUserEntity();
            list.add(systemUserEntity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
        return false;
    }
}

